﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Define Asset Types, Statuses and Custom Fields
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentMenuIDContent" runat="server">configMenu</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <link rel="stylesheet" type="text/css" href="../../Content/jquery-ui-1.8.16.custom.css" />
    <script type="text/javascript" src="../../Scripts/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.tablesorter.min.js")%>"></script>
    <script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.tablesorter.pager.js")%>"></script>
    <script type="text/javascript" src="../../Scripts/site.js"></script>
    <script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
    <script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script>
    <script src="<%= Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#miniTabs").tabs();
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Define Asset Types, Statuses and Custom Fields</h2>
    <br />
    <!-- the tabs -->
    <div id="miniTabs">
    <ul class="tabs">
        <li><a href="#assetTypeTab">Asset Types</a></li>
        <li><a href="#assetSatusTab">Asset Statuses</a></li>
        <li><a href="#customFieldTab">Asset Custom Fields</a></li>
    </ul>
    <!-- tab "panes" -->
        <div id="assetTypeTab">
            <% Using (Ajax.BeginForm("SaveAssetType", New AjaxOptions With {.UpdateTargetId = "typeContent", .OnBegin = "falidateAssetType", .OnSuccess = "clearAssetTypeSelection"}))%>
            <table>
                <tr>
                    <td align="right" width="220px">
                        <%= Html.Hidden("currAssetTypeID",0)%>
                        <%= Html.TextBox("txtTypeDesc", Nothing, New With {.class = "text wide-input"})%>
                    </td>
                    <td align="left" width="50px">
                        <input type="submit" class="button middle-button" value="Save" />
                    </td>
                    <td width="*" align="left">
                        <span id="typeErrors"></span>&nbsp;
                    </td>
                </tr>
                <tr>
                    <td colspan="3" id="typeContent">
                        <% Html.RenderPartial("~/Views/Configuration/Controls/AssetTypeList.ascx", ViewData("AssetTypes"))%>
                    </td>
                </tr>
            </table>
            <% End Using%>
            <table>
                <tr>
                    <td width="900px">
                        <input type="submit" class="button middle-button" value="Show History" onclick="showHistory('AssetType','assetTypeHistoryContent');" />
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td height="200" id="assetTypeHistoryContent" class="top">
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
            </table>
        </div>
        <div id="assetSatusTab">
            <% Using (Ajax.BeginForm("SaveAssetStatus", New AjaxOptions With {.UpdateTargetId = "statusContent", .OnBegin = "falidateAssetStatus", .OnSuccess = "clearAssetStatusSelection"}))%>
            <table>
                <tr>
                    <td align="right" width="250px">
                        <%= Html.Hidden("currAssetStatusID", 0)%>
                        <%= Html.TextBox("txtStatusDesc", Nothing, New With {.class = "text wide-input"})%>
                    </td>
                    <td align="left" width="50px">
                        <input type="submit" class="button middle-button" value="Save" />
                    </td>
                    <td width="*" align="left">
                        <span id="statusErrors"></span>&nbsp;
                    </td>
                </tr>
                <tr>
                    <td colspan="3" id="statusContent">
                        <% Html.RenderPartial("~/Views/Configuration/Controls/AssetStatusList.ascx", ViewData("AssetStatuses"))%>
                    </td>
                </tr>
            </table>
            <% End Using%>
            <table>
                <tr>
                    <td width="900px">
                        <input type="submit" class="button middle-button" value="Show History" onclick="showHistory('AssetStatus','assetStatusHistoryContent');" />
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td id="assetStatusHistoryContent" height="200" class="top">
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
            </table>
        </div>
        <div id="customFieldTab">
            <h3>
                Predefined Fields</h3>
            <% Using (Ajax.BeginForm("SaveCustomFields", New AjaxOptions With {.UpdateTargetId = "status"}))%>
            <table id="customFieldTable">
                <tr>
                    <th>
                        Field Name
                    </th>
                    <th>
                        Mandatory?
                    </th>
                    <th colspan="2">
                        &nbsp;
                    </th>
                </tr>
                <% For Each item In ViewData("PredefinedFields")%>
                <tr>
                    <td>
                        <%= item.Description%>
                    </td>
                    <td>
                        <%= Html.CheckBox(String.Format("cbxMandatoryPredefinedField_{0}", CInt(item.Type)), CBool(item.Selected))%>
                    </td>
                    <td colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <% Next%>
                <tr>
                    <td colspan="4">
                        <br />
                        <h3>
                            Custom Text Fields</h3>
                    </td>
                </tr>
                <tr>
                    <th>
                        Use this field
                    </th>
                    <th>
                        Field Name
                    </th>
                    <th>
                        Mandatory?
                    </th>
                    <th>
                        &nbsp;
                    </th>
                </tr>
                <% For Each item In ViewData("CustomTextFields")%>
                <tr>
                    <td>
                        <%= Html.CheckBox(String.Format("cbxUseCustomTextField_{0}", item.SequenceID), CBool(item.IsActive), New With {.onclick = String.Format("checkMondatoryText('{0}');", item.SequenceID)})%>
                        Custom Text Field
                        <%= item.SequenceID%>
                    </td>
                    <td>
                        <%= Html.TextBox(String.Format("txtCustomTextField_{0}", item.SequenceID), item.FieldName, New With {.class = "text"})%>
                    </td>
                    <td>
                        <%= Html.CheckBox(String.Format("cbxMandatoryCustomTextField_{0}", item.SequenceID), CBool(item.Mandatory), New With {.onclick = String.Format("checkMondatoryText('{0}');", item.SequenceID)})%>
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <% Next%>
                <tr>
                    <th colspan="4">
                        <br />
                        <h3>
                            Custom Dropdown Fields</h3>
                    </th>
                </tr>
                <tr>
                    <th>
                        Use this field
                    </th>
                    <th>
                        Field Name
                    </th>
                    <th>
                        Enter values separated by ;
                    </th>
                    <th>
                        Mandatory?
                    </th>
                </tr>
                <% For Each item In ViewData("CustomDDFields")%>
                <tr>
                    <td>
                        <%= Html.CheckBox(String.Format("cbxUseCustomDDField_{0}", item.SequenceID), CBool(item.IsActive), New With {.onclick = String.Format("checkMondatoryDD('{0}');", item.SequenceID)})%>
                        Custom DD Field&nbsp;
                        <%= item.SequenceID%>
                    </td>
                    <td>
                        <%= Html.TextBox(String.Format("txtCustomDDField_{0}", item.SequenceID), item.FieldName, New With {.class = "text"})%>
                    </td>
                    <td>
                        <%= Html.TextBox(String.Format("txtCustomDDFieldValues_{0}", item.SequenceID), item.Values, New With {.class = "text"})%>
                    </td>
                    <td>
                        <%= Html.CheckBox(String.Format("cbxMandatoryCustomDDField_{0}", item.SequenceID), CBool(item.Mandatory), New With {.onclick = String.Format("checkMondatoryDD('{0}');", item.SequenceID)})%>
                    </td>
                </tr>
                <% Next%>
                <tr>
                    <td colspan="4" align="center">
                    <br />
                        <p id="status" class="left">
                        </p>
                        <p class="right">
                            <input type="submit" class="button middle-button" value="Save" />
                        </p>
                    </td>
                </tr>
            </table>
            <% End Using%>
            <table>
                <tr>
                    <td width="900px">
                        <input type="submit" class="button middle-button" value="Show History" onclick="showHistory('AssetCustomField','assetCustomFieldHistoryContent');" />
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td id="assetCustomFieldHistoryContent" height="200" class="top">
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
            </table>
        </div>
        </div>
    <div id="detailDialog" title="History Detail" class="hidden">
<span>Loading...</span>
</div>
    <script type="text/javascript">

        function checkMondatoryText(id) {
            var isChecked = $("#cbxUseCustomTextField_" + id).is(':checked');
            if (isChecked === false)
                $("#cbxMandatoryCustomTextField_" + id).removeAttr("checked");
            return isChecked;
        }

        function checkMondatoryDD(id) {
            var isChecked = $("#cbxUseCustomDDField_" + id).is(':checked');
            if (isChecked === false)
                $("#cbxMandatoryCustomDDField_" + id).removeAttr("checked");
            return isChecked;
        }

        function setCurrentAssetType(id, desc) {
            clearAssetTypeSelection();
            $("#txtTypeDesc").val(desc);
            $("#currAssetTypeID").val(id);
            $("#assTypeRow_" + id).addClass("selected");
        }

        function clearAssetTypeSelection() {
            $('#assetTypeTable tbody tr').each(function (index) {
                $(this).removeClass("selected");
                $("#txtTypeDesc").val("");
                $("#currAssetTypeID").val(0);
            });
        }

        function setCurrentAssetStatus(id, desc) {
            $("#txtStatusDesc").val(desc);
            $("#currAssetStatusID").val(id);
            clearAssetStatusSelection();
            $("#assStatRow_" + id).addClass("selected");
        }

        function clearAssetStatusSelection() {
            $('#assetStatusTable tbody tr').each(function (index) {
                $(this).removeClass("selected");
            });
            $("select#DDAssetStatus").change(function () {
                changeActiveAssetStatus();
            });
        }

        function changeActiveAssetStatus() {
            var id = $("#DDAssetStatus").val();
            jQuery.ajax({
                type: 'POST',
                data: { assetStatusID: id },
                url: '<%: Url.Action("SetAssetStatusAsActive") %>',
                success: function (result) {
                    $('#statusContent').html(result);
                    $("select#DDAssetStatus").change(function () {
                        changeActiveAssetStatus();
                    });
                }
            });
        }

        function showHistory(entityType, contentIDToUpdate) {
            showLoading("miniTabs", true);
            jQuery.ajax({
                type: 'POST',
                data: { entityType: entityType, pageNo: 1 },
                url: '<%= Url.Content("~/History") %>',
                success: function (result) {
                    $('#' + contentIDToUpdate).html(result);
                    $("td#" + contentIDToUpdate + " > table").tablesorter({ widthFixed: true, widgets: ['zebra'] })
                    .tablesorterPager({ container: $("td#" + contentIDToUpdate + " > span"), positionFixed: false });
                    hideLoading("miniTabs");
                }
            });
        }

        function falidateAssetType() {
            $("#typeMsg").html("");
            return $(document.forms[0]).validate({
                rules: {
                    "txtTypeDesc": { required: true }
                },
                messages: {
                    "txtTypeDesc": "Description is required."
                },
                errorLabelContainer: '#typeErrors'
            }).form();
        }

        function falidateAssetStatus() {
            $("#statusMsg").html("");
            return $(document.forms[1]).validate({
                rules: {
                    "txtStatusDesc": { required: true }
                },
                messages: {
                    "txtStatusDesc": "Description is required."
                },
                errorLabelContainer: '#statusErrors'
            }).form();
        }

        var detailDialog = {
            show: function (id) { showDetail(id); },
            close: function () { closeDialog(); },
            OnSuccess: null
        }


        function closeDialog() {
            $("#detailDialog").dialog("close");
        }

        function showDetail(id) {
            $('#detailDialog').html("<span>Loading...</span>");

            $("#detailDialog").dialog({
                modal: true,
                width: 600,
                height: 400,
                zIndex: 100
            });

            jQuery.ajax({
                type: 'POST',
                data: { id: id },
                url: '<%= Url.Content("~/History/Detail") %>',
                success: function (result) {
                    $('#detailDialog').html(result);
                }
            });

        }


        $(document).ready(function () {
            $("select#DDAssetStatus").change(function () {
                changeActiveAssetStatus();
            });
        });

    </script>
</asp:Content>
